/**
 * (c): IML, IFAK.
 *
 */
package org.opentcs.guing.transport;

import java.util.Objects;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.opentcs.data.TCSObjectReference;
import org.opentcs.data.model.Vehicle;
import org.opentcs.data.order.OrderSequence;
import org.opentcs.data.order.TransportOrder;
import org.opentcs.guing.components.dialogs.DialogContent;
import org.opentcs.guing.exchange.DefaultKernelProxyManager;
import org.opentcs.guing.exchange.KernelProxyManager;

/**
 * Die Ansicht einer Transportauftragskette.
 * 
 * @author Heinz Huber (Fraunhofer IML)
 */
public class OrderSequenceView
    extends DialogContent {

  /**
   * Die anzuzeigende Transportauftragskette.
   */
  private final OrderSequence fOrderSequence;
  /**
   * The proxy/connection manager to be used.
   */
  private final KernelProxyManager kernelProxyManager;

  /**
   * Creates new instance.
   *
   * @param sequence The order sequence
   */
  public OrderSequenceView(OrderSequence sequence) {
    fOrderSequence = Objects.requireNonNull(sequence, "sequence is null");
    this.kernelProxyManager = DefaultKernelProxyManager.instance();
    initComponents();
    initFields();
    setDialogTitle("Transportauftragskette Detailansicht");
  }

  /**
   * Returns the order sequence.
   *
   * @return The order sequence.
   */
  public OrderSequence getOrderSequence() {
    return fOrderSequence;
  }

  @Override
  public void update() {
    // Do nada.
  }

  @Override
  public final void initFields() {
    // Allgemeine Angaben: ID
    int id = getOrderSequence().getId();
    textFieldId.setText(Integer.toString(id));
    // Name
    String name = getOrderSequence().getName();
    textFieldName.setText(name);

    boolean complete = getOrderSequence().isComplete();
    checkBoxComplete.setSelected(complete);
    checkBoxComplete.setEnabled(!complete);

    boolean finished = getOrderSequence().isFinished();
    checkBoxFinished.setSelected(finished);

    boolean failureFatal = getOrderSequence().isFailureFatal();
    checkBoxFailureFatal.setSelected(failureFatal);

    int finishedIndex = getOrderSequence().getFinishedIndex();
    textFieldFinishedIndex.setText("" + finishedIndex);

    TCSObjectReference<Vehicle> intendedVehicle = getOrderSequence().getIntendedVehicle();

    if (intendedVehicle != null) {
      textFieldIntendedVehicle.setText(intendedVehicle.getName());
    }

    TCSObjectReference<Vehicle> processingVehicle = getOrderSequence().getProcessingVehicle();

    if (processingVehicle != null) {
      textFieldProcessingVehicle.setText(processingVehicle.getName());
    }

//    TCSObjectReference<TransportOrder> nextUnfinishedOrder = getOrderSequence().getNextUnfinishedOrder();
    // TODO...
    // Transport orders
    DefaultTableModel tableModel = new UneditableTableModel();
    tableModel.setColumnIdentifiers(new String[] {"Name"});	// , "Quelle", "Ziel"...

    for (TCSObjectReference<TransportOrder> to : getOrderSequence().getOrders()) {
      String[] row = new String[1];
      row[0] = to.getName();
      tableModel.addRow(row);
    }

    transportOrdersTable.setModel(tableModel);
  }

  // CHECKSTYLE:OFF
  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        generalPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        labelId = new javax.swing.JLabel();
        textFieldId = new javax.swing.JTextField();
        labelName = new javax.swing.JLabel();
        textFieldName = new javax.swing.JTextField();
        labelFinishedIndex = new javax.swing.JLabel();
        textFieldFinishedIndex = new javax.swing.JTextField();
        labelIntendedVehicle = new javax.swing.JLabel();
        textFieldIntendedVehicle = new javax.swing.JTextField();
        labelProcessingVehicle = new javax.swing.JLabel();
        textFieldProcessingVehicle = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        checkBoxComplete = new javax.swing.JCheckBox();
        checkBoxFinished = new javax.swing.JCheckBox();
        checkBoxFailureFatal = new javax.swing.JCheckBox();
        transportOrdersPanel = new javax.swing.JPanel();
        transportOrdersScrollPane = new javax.swing.JScrollPane();
        transportOrdersTable = new javax.swing.JTable();

        setLayout(new java.awt.GridBagLayout());

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/opentcs/guing/res/labels"); // NOI18N
        generalPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("OrderSequenceView.generalPanel.title"))); // NOI18N
        generalPanel.setLayout(new java.awt.GridBagLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        labelId.setFont(labelId.getFont());
        labelId.setText("Id:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
        jPanel1.add(labelId, gridBagConstraints);

        textFieldId.setColumns(10);
        textFieldId.setEditable(false);
        textFieldId.setFont(textFieldId.getFont());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(textFieldId, gridBagConstraints);

        labelName.setFont(labelName.getFont());
        labelName.setText("Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
        jPanel1.add(labelName, gridBagConstraints);

        textFieldName.setColumns(10);
        textFieldName.setEditable(false);
        textFieldName.setFont(textFieldName.getFont());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(textFieldName, gridBagConstraints);

        labelFinishedIndex.setFont(labelFinishedIndex.getFont());
        labelFinishedIndex.setText(bundle.getString("OrderSequenceView.finishedIndex.text")); // NOI18N
        labelFinishedIndex.setToolTipText(bundle.getString("OrderSequenceView.finishedIndex.toolTipText")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
        jPanel1.add(labelFinishedIndex, gridBagConstraints);

        textFieldFinishedIndex.setColumns(10);
        textFieldFinishedIndex.setEditable(false);
        textFieldFinishedIndex.setFont(textFieldFinishedIndex.getFont());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(textFieldFinishedIndex, gridBagConstraints);

        labelIntendedVehicle.setFont(labelIntendedVehicle.getFont());
        labelIntendedVehicle.setText(bundle.getString("OrderSequenceView.intendedVehicle")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
        jPanel1.add(labelIntendedVehicle, gridBagConstraints);

        textFieldIntendedVehicle.setColumns(10);
        textFieldIntendedVehicle.setEditable(false);
        textFieldIntendedVehicle.setFont(textFieldIntendedVehicle.getFont());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(textFieldIntendedVehicle, gridBagConstraints);

        labelProcessingVehicle.setFont(labelProcessingVehicle.getFont());
        labelProcessingVehicle.setText(bundle.getString("OrderSequenceView.executingVehicle")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
        jPanel1.add(labelProcessingVehicle, gridBagConstraints);

        textFieldProcessingVehicle.setColumns(10);
        textFieldProcessingVehicle.setEditable(false);
        textFieldProcessingVehicle.setFont(textFieldProcessingVehicle.getFont());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(textFieldProcessingVehicle, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.5;
        generalPanel.add(jPanel1, gridBagConstraints);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        checkBoxComplete.setText(bundle.getString("OrderSequenceView.complete.text")); // NOI18N
        checkBoxComplete.setToolTipText(bundle.getString("OrderSequenceView.complete.toolTipText")); // NOI18N
        checkBoxComplete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxCompleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel2.add(checkBoxComplete, gridBagConstraints);

        checkBoxFinished.setText(bundle.getString("OrderSequenceView.finished")); // NOI18N
        checkBoxFinished.setToolTipText(bundle.getString("OrderSequenceView.finished.tooltip")); // NOI18N
        checkBoxFinished.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel2.add(checkBoxFinished, gridBagConstraints);

        checkBoxFailureFatal.setText(bundle.getString("OrderSequenceView.failureFatal")); // NOI18N
        checkBoxFailureFatal.setToolTipText(bundle.getString("OrderSequenceView.failureFatal.tooltip")); // NOI18N
        checkBoxFailureFatal.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel2.add(checkBoxFailureFatal, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        generalPanel.add(jPanel2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        add(generalPanel, gridBagConstraints);

        transportOrdersPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("OrderSequenceView.transportOrders"))); // NOI18N
        transportOrdersPanel.setLayout(new java.awt.GridBagLayout());

        transportOrdersScrollPane.setPreferredSize(new java.awt.Dimension(100, 100));
        transportOrdersScrollPane.setViewportView(transportOrdersTable);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        transportOrdersPanel.add(transportOrdersScrollPane, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(transportOrdersPanel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

	private void checkBoxCompleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBoxCompleteActionPerformed
    int n = JOptionPane.showConfirmDialog(this,
                                          "Transportauftrags-Kette als komplett markieren?",
                                          "Auftragskette",
                                          JOptionPane.YES_NO_OPTION);

    if (n == JOptionPane.OK_OPTION) {
      kernelProxyManager.kernel().setOrderSequenceComplete(fOrderSequence.getReference());
      checkBoxComplete.setEnabled(false);
    }
    else {
      checkBoxComplete.setSelected(false);
    }
	}//GEN-LAST:event_checkBoxCompleteActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox checkBoxComplete;
    private javax.swing.JCheckBox checkBoxFailureFatal;
    private javax.swing.JCheckBox checkBoxFinished;
    private javax.swing.JPanel generalPanel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel labelFinishedIndex;
    private javax.swing.JLabel labelId;
    private javax.swing.JLabel labelIntendedVehicle;
    private javax.swing.JLabel labelName;
    private javax.swing.JLabel labelProcessingVehicle;
    private javax.swing.JTextField textFieldFinishedIndex;
    private javax.swing.JTextField textFieldId;
    private javax.swing.JTextField textFieldIntendedVehicle;
    private javax.swing.JTextField textFieldName;
    private javax.swing.JTextField textFieldProcessingVehicle;
    private javax.swing.JPanel transportOrdersPanel;
    private javax.swing.JScrollPane transportOrdersScrollPane;
    private javax.swing.JTable transportOrdersTable;
    // End of variables declaration//GEN-END:variables
  // CHECKSTYLE:ON

}
